#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>

#include "httpclient.h"

namespace {
const QString g_url{"http://httpbin.org"};
constexpr const char* g_test{"https://baidu.com"};
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    OnInitDialog();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::OnInitDialog()
{
    if(nullptr == m_http)
        m_http = new HttpClient(this);
    Q_ASSERT(m_http);
    connect(m_http,&HttpClient::signal_get_finished,[this](const DefHttpResult&result){
        qDebug()<<"Async Get Response: "<<result.response<<" Status Code: "<<result.status_code<<" response length: "<<result.response.length()<<"\r\n";
    });
    connect(m_http,&HttpClient::signal_post_finished,[this](const DefHttpResult&result){
        qDebug()<<"Async Post Response: "<<result.response<<" Status Code: "<<result.status_code<<" response length: "<<result.response.length()<<"\r\n";
    });

    auto get_btn = new QPushButton("get",this);
    get_btn->move(200,280);

    auto post_btn = new QPushButton("post",this);
    post_btn->move(350,280);

    connect(get_btn,&QPushButton::clicked,this,[this]{
        if(m_http)
            m_http->HttpGetAsync(QString("%1/get").arg(g_url));
    });
    connect(post_btn,&QPushButton::clicked,this,[this]{
           //QByteArray postData = R"({"name": "test", "value": 123})";
        QByteArray postData = R"({"id":"1001","xml":"<?xml version="1.0" encoding= "GBK"?><root><QueryCondition><ip>192.168.110.135</ip><mac>00:0C:29:8E:41:06</mac><version>5.0.1.5</version><kskm>2</kskm></QueryCondition></root>")";
           if(m_http)
               m_http->HttpPostAsync(QString("%1/post").arg(g_url),postData);
    });
}
